package com.yuyou.tas.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yuyou.tas.admin.service.IAuthOrganizationFuncService;
import com.yuyou.tas.admin.service.IAuthRoleService;
import com.yuyou.tas.admin.service.ILoginService;
import com.yuyou.tas.admin.service.IOfficeTokenService;
import com.yuyou.tas.service.api.admin.dto.OfficeTokenDTO;
import com.yuyou.tas.service.api.lesson.service.LessonClassAPI;
import com.yuyou.tas.service.api.lesson.service.LessonResourceAPI;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ReturnResult;

@Api(value="OfficeTokenController",description="OFFICE在线编辑功能")
@Validated
@RestController
@RequestMapping(value="/api/token")
public class OfficeTokenController extends BaseController{

	private final String APP_ID="0001";
		
	@Autowired
	private ILoginService loginService;
	
	@Autowired
	private LessonResourceAPI lessonResourceAPI;
	
	@Autowired
	private LessonClassAPI lessonClassAPI;
	
	@Autowired
	private IAuthRoleService authRoleService;
	
	@Autowired
	private IOfficeTokenService officeTokenService;

	
	@ApiOperation(value="上传office文件成功")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "resourceId", value = "资源ID", paramType = "query", required = true) })
	@RequestMapping(value="/upload" ,method=RequestMethod.POST)
	public ReturnResult<OfficeTokenDTO> upload(HttpServletRequest request,@NotNull(message="resourceId不能为空") String resourceId) throws Exception{
		LoginRedisObj user = this.getUserObj(request);	
		ResBoolSimpleInfo<String> res=lessonResourceAPI.addResourceTemp(resourceId, user.getOrganizationId(), null, user.getUserId());		
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(),null);
	}
	
	
	@ApiOperation(value="获取office预览权限的token")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "resourceId", value = "资源ID", paramType = "query", required = true) })
	@RequestMapping(value="/view" ,method=RequestMethod.POST)
	public ReturnResult<OfficeTokenDTO> view(HttpServletRequest request,@NotNull(message="resourceId不能为空") String resourceId) throws Exception{
		LoginRedisObj user = this.getUserObj(request);	
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		res.setSuccess(true);
		OfficeTokenDTO officeTokenDTO=officeTokenService.view(user,resourceId);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(),officeTokenDTO);
	}
	
	
	@ApiOperation(value="获取office编辑权限的token")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "resourceId", value = "资源ID", paramType = "query", required = true) })
	@RequestMapping(value="/edit" ,method=RequestMethod.POST)
	public ReturnResult<OfficeTokenDTO> edit(HttpServletRequest request,@NotNull(message="resourceId不能为空") String resourceId) throws Exception{
		LoginRedisObj user = this.getUserObj(request);	
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		res.setSuccess(true);
		OfficeTokenDTO officeTokenDTO=officeTokenService.edit(user,resourceId);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(),officeTokenDTO);
	}
	
	@ApiOperation(value="获取office打印权限的token")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "resourceId", value = "资源ID", paramType = "query", required = true) })
	@RequestMapping(value="/print" ,method=RequestMethod.POST)
	public ReturnResult<OfficeTokenDTO> print(HttpServletRequest request,@NotNull(message="resourceId不能为空") String resourceId) throws Exception{
		LoginRedisObj user = this.getUserObj(request);	
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		res.setSuccess(true);
		OfficeTokenDTO officeTokenDTO=officeTokenService.print(user,resourceId);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(),officeTokenDTO);
	}
	
	@ApiOperation(value="获取office下载权限的token")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "resourceId", value = "资源ID", paramType = "query", required = true) })
	@RequestMapping(value="/download" ,method=RequestMethod.POST)
	public ReturnResult<OfficeTokenDTO> download(HttpServletRequest request,@NotNull(message="resourceId不能为空") String resourceId) throws Exception{
		LoginRedisObj user = this.getUserObj(request);	
		ResBoolSimpleInfo<String> res=new ResBoolSimpleInfo<String>();
		res.setSuccess(true);
		OfficeTokenDTO officeTokenDTO=officeTokenService.download(user,resourceId);
		return ReturnResult.returnRes(res.isSuccess(), res.getMessage(),officeTokenDTO);
	}

}
